<template>
  <div>
    <a-row>
      <a-col :offset="6" :span="8">
        <a-card title="新增客户">
          <a-form v-bind="layout">
            <a-form-item label="客户名称" required="true" v-bind="validateInfos.name">
              <a-input v-model:value="addCustomerData.name" />
            </a-form-item>
            <a-form-item label="联系人" required="true" v-bind="validateInfos.contact">
              <a-input v-model:value="addCustomerData.contact" />
            </a-form-item>

            <a-form-item label="联系方式" v-bind="validateInfos.mobile">
              <a-input v-model:value="addCustomerData.mobile" />
            </a-form-item>

            <a-form-item label="地址" v-bind="validateInfos.address">
              <a-input v-model:value="addCustomerData.address" />
            </a-form-item>

            <a-form-item label="业务开始时间" required v-bind="validateInfos.startedAt">
              <a-date-picker v-model:value="addCustomerData.startedAt" :showToday="true" />
            </a-form-item>
            <a-form-item label="备注">
              <a-input v-model:value="addCustomerData.remark" />
            </a-form-item>

            <a-form-item :wrapper-col="{ span: 14, offset: 8 }">
              <a-button type="primary" @click="add">确定</a-button>
              <a-button style="margin-left: 30px" @click="resetFields">重置</a-button>
            </a-form-item>
          </a-form>
        </a-card>
      </a-col>
    </a-row>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive } from 'vue'
import { AddCustomerData } from '@/api/chunfeng/customer/interfaces'
import { useForm } from '@ant-design-vue/use'
import { useHttpRequest } from '@/api/chunfeng/apiRequest'
import { useCustomerApi } from '@/api/chunfeng/customer'
import moment from 'moment'

export default defineComponent({
  name: 'AddForm',
  props: {
    title: {
      required: true,
      type: 'string',
    },
  },
  setup() {
    const { executeCommandRequest } = useHttpRequest()
    const { addCustomer } = useCustomerApi()

    const addCustomerData = reactive({
      name: '',
      contact: '',
      mobile: undefined,
      address: undefined,
      remark: undefined,
      startedAt: moment(),
    })
    const rules = reactive({
      name: [
        {
          required: true,
          message: '请输入客户名称',
        },
      ],
      contact: [
        {
          required: true,
          message: '请输入客户名称',
        },
      ],
      startedAt: [
        {
          required: true,
          type: 'date',
          message: '请选择业务开始日期',
        },
      ],
      mobile: [
        {
          required: false,
        },
      ],
      address: [
        {
          required: false,
        },
      ],
      remark: [
        {
          required: false,
        },
      ],
    })

    const { resetFields, validate, validateInfos } = useForm(addCustomerData, rules)
    const layout = {
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
    }

    function add() {
      validate<AddCustomerData>().then((data) => {
        executeCommandRequest(addCustomer(data), {
          message: '添加客户成功',
          callback: () => {
            resetFields()
          },
        })
      })
    }
    return {
      addCustomerData,
      validateInfos,
      layout,
      add,
      resetFields,
    }
  },
})
</script>
